﻿$(function () {
    var _url = "/ajaxdata/Activity/";
    var lid = $("input[name=lid]").val();

    if (lid != "") {
        $.ajax({
            url: _url,
            data: { action: "get", id: lid },
            dataType: "json",
            success: function (data) {
                var title_time_str = data.Title.split('^');
                $("input[name=title]").val(title_time_str[0]);
                $("input[name=starttime]").val(title_time_str[1]);
                $("input[name=endtime]").val(title_time_str[2]);
                $("input[name=address]").val(data.Address);
                $("textarea[name=shortdes]").val(data.Profile);
                $("select[name=host]").val(data.HostID);
                $.each(data.Imgs.split(','), function (i, val) {
                    if (val.replace(/#/g, "") != "" && val.indexOf("no_photo.gif") < 0) {
                        $(".Carousel img").eq(i).attr("src", val);
                    }
                });

            }

        });
    }


    $("#save").click(function () {
        var title = $.trim($("input[name=title]").val());
        if (title == "") {
            alert("请输入标题。")
            return false;
        }

        $(".Carousel img").each(function (i) {
            $("input[name=crs_img]").eq(i).val($(this).attr("src"));
        });
        var postdata = $(".webform").serializeArray();
        postdata.push({ name: "action", value: "live_save" });
        $.ajax({
            url: _url,
            type: "POST",
            dataType:"json",
            data: postdata,
            success: function (data) {
                if (confirm("提交成功，现在添加直播内容")) {
                    location = "detail_post.aspx?lid=" + data.lid;
                } else {
                    location = "list.aspx";
                }
            },
            error: function (req) {
                alert("提交出错。");
            },
            beforeSend: function () {
                $("#save,.loading2").toggle();
            },
            complete: function () {
                $("#save,.loading2").toggle();
            }
        });
    });



    $(".selimg").click(function () {
        var imgurl = window.prompt("请输入图地址。");
        if (imgurl != null) {
            $(".imgwrap img").eq($(".selimg").index(this)).attr("src", imgurl);
        }
    });

})